/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
Uploader = function () {
    var me = this;
    me.UPLOAD_TYPE = {
        XHR: 'xhr',
        FLASH: 'flash'
    };
    me._uploadType = me.UPLOAD_TYPE.XHR;
    me.init({});
}

Uploader.prototype = {

    init: function (config) {
        var uploader = null;
        // SWFUpload
        // setting BEGIN
        var settings = {
            flash_url: "js/lib/swfupload/swfupload.swf",
            flash9_url: "js/lib/swfupload/swfupload_fp9.swf",
            upload_url: "upload",
            use_query_string: false, /* post_params 定义参数是否作为 queryStirng */
            file_size_limit: "100MB",
            file_types: "*.*",
            file_types_description: "支持的文件类型",
            button_text: "Select Files",//document.getElementById('f'),
            button_placeholder_id: "f",
            //button_image_url: "feature/select_files.png",
            button_width: 100,//e.flash.buttonWidth,
            button_height: 20,
            button_action: SWFUpload.BUTTON_ACTION.SELECT_FILES,
            button_cursor: SWFUpload.CURSOR.HAND,
            button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
            /* 文件选取窗口将要弹出时触发 */
            file_dialog_start_handler: function () {
                console.log('select files');
                /*
                 t.checkUploadToPhone() && this.setUploadURL(Airdroid.Service.getFlashUploadPath()),
                 */
            },
            /* 选择完毕 */
            file_dialog_complete_handler: function (selecteds/*本次选择总数量*/, queueds/*本次添加到队列中数量*/, queuedTotal/*队里中总数量*/) {
                console.log("file_dialog_complete_handler"),
                console.log(selecteds),
                console.log(queueds),
                console.log(queuedTotal);
                // this.startUpload(); // upload
            },
            /* 添加到队列 */
            file_queued_handler: function (file) {
                //console.log("file_queued_handler"), console.log(file), $.isFunction(e.common.uploadStarted) && (e.common.uploadStarted(file, t._uploadType), t.setGA(file));
                console.log("file_queued_handler");
                console.log(file);
                this.startUpload();
            },
            /* 开始上传 */
            upload_start_handler: function (file) {

                if (/*t.checkAllowUpload(file)*/true) {
                    var params = {
                        loader: "2",
                        fname: file.name,
                        fpath: e.common.fpath,
                        after: e.common.afterAction || "0",
                        rtype: e.common.rtype || "0"
                    }, postParams = $.extend(!0, params, e.flash.param);
                    this.setPostParams(postParams)
                } else {
                    this.cancelUpload(file.id, !0);
                }
            },
            /* 上传进度 */
            upload_progress_handler: function (file, complete, total) {
                var r = Math.round(100 * (complete / total));
                $.isFunction(e.common.progressUpdated) && e.common.progressUpdated(file, r, t._uploadType)
                console.log(r);
            },
            /* 上传错误 */
            upload_error_handler: function (file, error, message) {
                console.log("flash upload error==>" + message),
                    $.isFunction(e.common.error) && e.common.error(file, t._commonLang.fail_to_upload, t._uploadType),
                    t.refreshDirUploadStates()
            },
            /* 上传成功 */
            upload_success_handler: function (file, data) {
                var o = $.parseJSON(data);
                1 == o.result ? $.isFunction(e.common.uploadFinished) && e.common.uploadFinished(file, o.data, t._uploadType) : $.isFunction(e.common.error) && e.common.error(file, t.getErrorMsgByCode(o.result), t._uploadType)
            }
        };
        // setting END
        uploader = new SWFUpload(settings);
        console.log('uploader initialed');
    }
}

new Uploader();
